package com.webcamtracker.ui;

import com.webcamtracker.ms2.ColorHistogramIndex;
import com.webcamtracker.ms2.SpaceTransforms;
import com.webcamtracker.ms2.TrackingFilter;

import javax.swing.*;
import java.awt.*;

public class UIColorHistogramIndex extends JPanel {
    private int barWidth = 5;
    private int bars;
    private ColorHistogramIndex colorHistogramIndex;
    private SpaceTransforms.SpaceCoordsTransform valueMapper;

    public void setColorHistogramIndex(ColorHistogramIndex colorHistogramIndex) {
        this.colorHistogramIndex = colorHistogramIndex;
        bars = -1;
    }

    class InnerUIColorHistogramIndex extends JPanel {
        public Dimension getPreferredSize() {
            return new Dimension(200, (int) (barWidth * (bars == -1 ? Math.pow(TrackingFilter.NUMBEROFBINS, 3) : bars)));
        }

        public void paint(Graphics graphics) {
            super.paint(graphics);
            if (colorHistogramIndex != null) {
                int y = 0;
                int countBars = 0;
                int width = barWidth;
                float[][][] rgbHistogram = colorHistogramIndex.getRgbHistogram();
                if (rgbHistogram != null) {
                    float check = 0.0f;
                    for (int i = 0; i < rgbHistogram.length; i++) {
                        for (int j = 0; j < rgbHistogram[i].length; j++) {
                            for (int k = 0; k < rgbHistogram[i][j].length; k++) {
                                int r = i * 255 / (rgbHistogram.length - 1);
                                int g = j * 255 / (rgbHistogram[i].length - 1);
                                int b = k * 255 / (rgbHistogram[i][j].length - 1);
                                Color color = new Color(r, g, b);
                                graphics.setColor(color);
                                graphics.fillRect(0, y, width, width);
                                check += rgbHistogram[i][j][k];
                                if (rgbHistogram[i][j][k] > 0) {
                                    int value = (int) valueMapper.map(rgbHistogram[i][j][k]);
                                    if (value >= 1) {
                                        graphics.fillRect(width, y, (int) value, width);
                                        y += width;
                                        countBars++;
                                    }
                                }
                            }
                        }
                    }
                    if (bars == -1) {
                        bars = countBars;
                        invalidate();
                    }
                    //System.out.println("*1 check = " + check);
                }
            }
        }
    }

    public UIColorHistogramIndex() {
        super();
        valueMapper = new SpaceTransforms.SpaceCoordsTransform(new SpaceTransforms.SpaceCoords(0, 0.10), new SpaceTransforms.SpaceCoords(0, 200));

        setLayout(new BorderLayout());
        InnerUIColorHistogramIndex innerUIColorHistogramIndex = new InnerUIColorHistogramIndex();
        JScrollPane jScrollPane = new JScrollPane(innerUIColorHistogramIndex);
        jScrollPane.setPreferredSize(new Dimension(200, 200));
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(jScrollPane);
    }
}
